using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.DirectoryServices;

namespace Microsoft.CommerceServer.SecureSite
{
	public class CreateIISApplicationPool : Microsoft.CommerceServer.SecureSite.Wizard
	{
		private System.Windows.Forms.GroupBox grpboxApplicationPool;
		private System.Windows.Forms.RadioButton rdobtnCreateApplicationPool;
		private System.Windows.Forms.RadioButton rdobtnExistingApplicationPool;
		private System.Windows.Forms.TextBox txtApplicationPool;
		private System.Windows.Forms.TreeView tvApplicationPool;
		private System.Windows.Forms.Label lblInstructions;
		private System.Windows.Forms.Panel pnlIISAppPool;
		private System.ComponentModel.IContainer components = null;

		public CreateIISApplicationPool()
		{
			// This call is required by the Windows Form Designer.
			InitializeComponent();
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.CreateIISApplicationPool_Load);
		}
		#endregion

		private void CreateIISApplicationPool_Load(object sender, System.EventArgs e)
		{
			this.pnlIISAppPool = new System.Windows.Forms.Panel();
			this.grpboxApplicationPool = new System.Windows.Forms.GroupBox();
			this.tvApplicationPool = new System.Windows.Forms.TreeView();
			this.txtApplicationPool = new System.Windows.Forms.TextBox();
			this.rdobtnExistingApplicationPool = new System.Windows.Forms.RadioButton();
			this.rdobtnCreateApplicationPool = new System.Windows.Forms.RadioButton();
			this.lblInstructions = new System.Windows.Forms.Label();
			this.pnlIISAppPool.SuspendLayout();
			this.grpboxApplicationPool.SuspendLayout();
			this.SuspendLayout();
			// 
			// btnNext
			// 
			this.btnNext.Name = "btnNext";
			this.btnNext.Click += new System.EventHandler(this.btnNext_Click);
			// 
			// btnPrevious
			// 
			this.btnPrevious.Name = "btnPrevious";
			this.btnPrevious.Click += new System.EventHandler(this.btnPrevious_Click);
			// 
			// pnlIISAppPool
			// 
			this.pnlIISAppPool.Controls.Add(this.grpboxApplicationPool);
			this.pnlIISAppPool.Controls.Add(this.lblInstructions);
			this.pnlIISAppPool.Dock = System.Windows.Forms.DockStyle.Fill;
			this.pnlIISAppPool.Location = new System.Drawing.Point(0, 72);
			this.pnlIISAppPool.Name = "pnlIISAppPool";
			this.pnlIISAppPool.Size = new System.Drawing.Size(552, 262);
			this.pnlIISAppPool.TabIndex = 4;
			// 
			// grpboxApplicationPool
			// 
			this.grpboxApplicationPool.Controls.Add(this.tvApplicationPool);
			this.grpboxApplicationPool.Controls.Add(this.txtApplicationPool);
			this.grpboxApplicationPool.Controls.Add(this.rdobtnExistingApplicationPool);
			this.grpboxApplicationPool.Controls.Add(this.rdobtnCreateApplicationPool);
			this.grpboxApplicationPool.Location = new System.Drawing.Point(48, 72);
			this.grpboxApplicationPool.Name = "grpboxApplicationPool";
			this.grpboxApplicationPool.Size = new System.Drawing.Size(456, 176);
			this.grpboxApplicationPool.TabIndex = 0;
			this.grpboxApplicationPool.TabStop = false;
			// 
			// tvApplicationPool
			// 
			this.tvApplicationPool.Enabled = false;
			this.tvApplicationPool.ImageIndex = -1;
			this.tvApplicationPool.Location = new System.Drawing.Point(200, 40);
			this.tvApplicationPool.Name = "tvApplicationPool";
			this.tvApplicationPool.SelectedImageIndex = -1;
			this.tvApplicationPool.Size = new System.Drawing.Size(224, 120);
			this.tvApplicationPool.TabIndex = 0;
			// 
			// txtApplicationPool
			// 
			this.txtApplicationPool.Location = new System.Drawing.Point(200, 16);
			this.txtApplicationPool.Name = "txtApplicationPool";
			this.txtApplicationPool.Size = new System.Drawing.Size(224, 20);
			this.txtApplicationPool.TabIndex = 0;
			this.txtApplicationPool.Text = "";
			// 
			// rdobtnExistingApplicationPool
			// 
			this.rdobtnExistingApplicationPool.Location = new System.Drawing.Point(8, 48);
			this.rdobtnExistingApplicationPool.Name = "rdobtnExistingApplicationPool";
			this.rdobtnExistingApplicationPool.Size = new System.Drawing.Size(176, 24);
			this.rdobtnExistingApplicationPool.TabIndex = 0;
			this.rdobtnExistingApplicationPool.Text = "Select Application Pool";
			this.rdobtnExistingApplicationPool.CheckedChanged += new System.EventHandler(this.rdobtnExistingApplicationPool_CheckedChanged);
			// 
			// rdobtnCreateApplicationPool
			// 
			this.rdobtnCreateApplicationPool.Checked = true;
			this.rdobtnCreateApplicationPool.Location = new System.Drawing.Point(8, 16);
			this.rdobtnCreateApplicationPool.Name = "rdobtnCreateApplicationPool";
			this.rdobtnCreateApplicationPool.Size = new System.Drawing.Size(176, 24);
			this.rdobtnCreateApplicationPool.TabIndex = 0;
			this.rdobtnCreateApplicationPool.TabStop = true;
			this.rdobtnCreateApplicationPool.Text = "Create Application Pool";
			this.rdobtnCreateApplicationPool.CheckedChanged += new System.EventHandler(this.rdobtnCreateApplicationPool_CheckedChanged);
			// 
			// lblInstructions
			// 
			this.lblInstructions.Location = new System.Drawing.Point(40, 16);
			this.lblInstructions.Name = "lblInstructions";
			this.lblInstructions.Size = new System.Drawing.Size(472, 48);
			this.lblInstructions.TabIndex = 0;
			// 
			// CreateIISApplicationPool
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(552, 374);
			this.Controls.Add(this.pnlIISAppPool);
			this.Name = "CreateIISApplicationPool";
			this.Controls.SetChildIndex(this.pnlIISAppPool, 0);
			this.pnlIISAppPool.ResumeLayout(false);
			this.grpboxApplicationPool.ResumeLayout(false);
			this.ResumeLayout(false);

			CommerceSecurityInfo.Log("FORM: CreateIISApplicationPool form loaded");
			base.Title = "Create IIS Applicaton Pool";
			this.lblInstructions.Text = "Security wizard will use user credentials previously supplied to create a new Application Pool Identity or set credentials to current selected IIS Application Pool. Click Next to continue.";

			// load application pools
			loadAppPools();
		}

		private void loadAppPools()
		{
			DirectoryEntry appPools = new DirectoryEntry("IIS://localhost/w3svc/AppPools");

			TreeNode appNode = tvApplicationPool.Nodes.Add("Application Pools");
			appNode.Tag = "Root";

			foreach(DirectoryEntry appPool in appPools.Children)
			{
				TreeNode AppPoolNode = appNode.Nodes.Add(appPool.Name);
				AppPoolNode.Tag = "AppPool";
			}
		}

		private void rdobtnExistingApplicationPool_CheckedChanged(object sender, System.EventArgs e)
		{
			if(rdobtnExistingApplicationPool.Checked == true)
			{
				txtApplicationPool.Enabled = false;
				tvApplicationPool.Enabled = true;
			}
		}

		private void rdobtnCreateApplicationPool_CheckedChanged(object sender, System.EventArgs e)
		{
			if(rdobtnCreateApplicationPool.Checked == true)
			{
				txtApplicationPool.Enabled = true;
				tvApplicationPool.Enabled = false;
			}
		}

		protected override void btnPrevious_Click(object sender, System.EventArgs e)
		{
			CommerceSecurityInfo.MoveToForm("CommerceAccount");
			base.Visible = false;
		}

		protected override void btnNext_Click(object sender, System.EventArgs e)
		{
			ValidateAppPool();
			if (base.IsValid)
			{
				if(this.rdobtnCreateApplicationPool.Checked==true)
				{
					CommerceSecurityInfo.ApplicationPoolName  = this.txtApplicationPool.Text;
				}
				else
				{
					CommerceSecurityInfo.ApplicationPoolName  = this.tvApplicationPool.SelectedNode.Text;
				}
				CommerceSecurityInfo.MoveToForm("CatalogWebSerice");
				base.Visible = false;
			}
		}

		private void ValidateAppPool()
		{
			if(this.rdobtnCreateApplicationPool.Checked==true)
			{
				TextBox box1 = this.txtApplicationPool;
				if (box1.Text == string.Empty)
				{
					this.MasterErrorProvider.SetError(box1, "Enter a name for the Application Pool");
				}
				else
				{
					this.MasterErrorProvider.SetError(box1, "");
				}
				CommerceSecurityInfo.NewApplicationPool = true;
			}
			else
			{
				this.MasterErrorProvider.SetError(txtApplicationPool, "");
				if(this.tvApplicationPool.SelectedNode!=null)
				{
					if(this.tvApplicationPool.SelectedNode.Tag.ToString()!="AppPool")
					{
						this.MasterErrorProvider.SetError(tvApplicationPool, "Select an Application Pool");
					}
					else
					{
						this.MasterErrorProvider.SetError(tvApplicationPool, "");
					}
				}
				else
				{
					this.MasterErrorProvider.SetError(tvApplicationPool, "Select an Application Pool");
				}
				CommerceSecurityInfo.NewApplicationPool = false;
			}
		}
	}
}

